package com.pan;


import com.pan.code.VerificationCode;
import com.pan.code.impl.ComputeVerificationCode;
import com.pan.code.impl.StringVerificationCode;
import com.pan.entity.Base64Result;
import com.pan.enumn.CodeEnum;

import java.util.ArrayList;
import java.util.List;



public class PanVerificationCode implements VerificationCode{

    private int type;

    private List<VerificationCode> verificationCodeList;

    public static VerificationCode createStringValidateCode(){
        return new StringVerificationCode();
    }

    public static VerificationCode createComputeValidateCode(){
        return new ComputeVerificationCode();
    }

    public PanVerificationCode(){
        this(CodeEnum.ANY_TYPE);
    }
    public PanVerificationCode(int type){
        this.type = type;
        verificationCodeList = new ArrayList<>();
        switch (type){
            case -1:
            case 0:
                verificationCodeList.add(PanVerificationCode.createStringValidateCode());
                if (type==0) break;
            case 1:
                verificationCodeList.add(PanVerificationCode.createComputeValidateCode());
        }

    }
    public PanVerificationCode(CodeEnum codeEnum){
        this(codeEnum.getType());
    }

    @Override
    public Base64Result getBase64() {
        int i = type==-1? (int) (Math.random() * type):type;
        return this.verificationCodeList.get(i).getBase64();
    }

    @Override
    public String get(String filePath, String fileName) {
        int i = type==-1? (int) (Math.random() * type):type;
        return this.verificationCodeList.get(i).get(filePath,fileName);
    }

    @Override
    public void close() {
        verificationCodeList.stream().forEach(VerificationCode::close);
    }
}
